package demo.msa.grpc.server;

import demo.msa.grpc.hello.api.HelloRequest;
import demo.msa.grpc.hello.api.HelloResponse;
import demo.msa.grpc.hello.api.HelloServiceGrpc;
import demo.msa.grpc.server.GrpcService;
import io.grpc.stub.StreamObserver;

@GrpcService
public class HelloServiceImpl extends HelloServiceGrpc.HelloServiceImplBase {
    @Override
    public void say(HelloRequest request, StreamObserver<HelloResponse> responseStreamObserver) {
        HelloResponse response = null;
        try {
            System.out.println("HelloServiceImpl threadid = [" + Thread.currentThread().getId() + "]");

            response = HelloResponse.newBuilder().setMessage("Hello " + request.getName()).build();
        } catch (Exception e) {
            responseStreamObserver.onError(e);
        } finally {
            responseStreamObserver.onNext(response);
        }

        responseStreamObserver.onCompleted();
    }
}
